package com.totowoo.shirodemo.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.totowoo.shirodemo.model.Permission;
import com.totowoo.shirodemo.service.PermissionService;

@Controller
public class PermissionController {

	private static final Logger log = LoggerFactory.getLogger(PermissionController.class);

	@Autowired
	private PermissionService permissionService;

	@GetMapping("/admin/permissionList.html")
	public String permissionList(Model model) {
		List<Permission> permissions = permissionService.getPermissionList();

		model.addAttribute("permissions", permissions);
		return "admin/permissionList";
	}

	@GetMapping("/admin/addPermission.html")
	public String addPermission(String id, Model model) {
		if(!StringUtils.isBlank(id)) {
			int pid = Integer.parseInt(id);
			Permission permission = permissionService.getPermissionById(pid);
		}
		return "/admin/addPermission";
	}

	@PostMapping("/admin/addPermission.html")
	public String savePermission(Permission permission, Model model) {
		if (permissionService.addPermission(permission) == null) {
			log.info("添加权限失败!");
			model.addAttribute("errMsg", "添加权限失败");
			model.addAttribute("permission", permission);
			return "/admin/addPermission";
		}

		return "redirect:/admin/permissionList.html";
	}
	
	@GetMapping("/admin/editPermission.html")
	public String editPermission(Model model) {
		return "/admin/editPermission";
	}

}
